package com.wiscamp.ninechapters.problems.adapter.resources.mistakes;

import com.wiscamp.ninechapters.problems.application.dto.commands.CreateMistakeCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.mistakes.CreateMistakesByReportCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.mistakes.MistakeQuantityBySetTypeDto;
import com.wiscamp.ninechapters.problems.application.dto.vo.mistakes.MistakeQuestionVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.sheets.MistakeVO;
import com.wiscamp.ninechapters.problems.application.services.MistakeAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/api/Problems")
@Tag(name = "题库-错题集信息")
public class MistakeController extends BaseController {
    private final MistakeAppService mistakeAppService;

    public MistakeController(MistakeAppService mistakeAppService) {
        this.mistakeAppService = mistakeAppService;
    }


    @PostMapping("Mistakes/Query")
    @Operation(summary = "分页获取错题列表")
    @HandleResourceException
    public RestResponse<Collection<MistakeVO>> getMistakesByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                  @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, mistakeAppService::getMistakesByQuery);
    }

    @GetMapping("Users/{userId}/Mistakes")
    @Operation(summary = "通过用户编号获取错题列表")
    @HandleResourceException
    public RestResponse<Collection<MistakeVO>> getMistakesByUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                      @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, mistakeAppService::getMistakesByUser);
    }

    @GetMapping("Users/{userId}/Mistakes/SetTypes")
    @Operation(summary = "通过编号获取错题明细信息")
    @HandleResourceException
    public RestResponse<Collection<MistakeQuantityBySetTypeDto>> getGroupedMistakeSet(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                                      @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, mistakeAppService::getGroupedMistakesQuantityBySetType);
    }

    @GetMapping("Mistakes/{mistakeId}/Question")
    @Operation(summary = "通过编号获取错题明细信息")
    @HandleResourceException
    public RestResponse<MistakeQuestionVO> getMistakeQuestion(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                              @PathVariable("mistakeId") long mistakeId) {
        return ResourceUtils.getById(request, response, mistakeId, mistakeAppService::getMistakeQuestion);
    }

    @PostMapping("Mistakes")
    @Operation(summary = "创建错题")
    @HandleResourceException
    public RestResponse<MistakeVO> createMistake(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody CreateMistakeCommand command) {
        var vo = mistakeAppService.createMistake(command);
        return ResourceUtils.getOkResponse(request, response, vo);
    }

    /**
     * 通过练习任务编号获取报告中错题并加入错题本
     *
     * @param request
     * @param response
     * @param taskId
     * @return
     */
    @PostMapping ("Mistakes/Actions/Create/By/Report")
    @Operation(summary = "通过练习任务编号获取报告中错题并加入错题本")
    @HandleResourceException
    public RestResponse<Boolean> createMistakesByReport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                        @RequestBody CreateMistakesByReportCommand command) {
        var result = mistakeAppService.createMistakesByReport(command);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    @GetMapping ("Tasks/{taskId}/Report/Actions/Create/Mistakes")
    @Operation(summary = "通过练习任务编号获取报告中错题并加入错题本")
    @HandleResourceException
    public RestResponse<Boolean> createMistakesByReport(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                        @PathVariable("taskId") long taskId) {
        var command = new CreateMistakesByReportCommand();
        command.setTaskId(taskId);
        var result = mistakeAppService.createMistakesByReport(command);
        return ResourceUtils.getOkResponse(request, response, result);
    }

    @DeleteMapping("Mistakes")
    @Operation(summary = "通过编号删除错题")
    @HandleResourceException
    public RestResponse<Boolean> deleteMistake(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, mistakeAppService::deleteMistake);
    }

    @DeleteMapping("Mistakes/{mistakeId}")
    @Operation(summary = "通过编号删除错题")
    @HandleResourceException
    public RestResponse<Boolean> deleteMistakeById(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                               @PathVariable("mistakeId") long mistakeId) {
        var command = new DeleteCommand();
        command.setId(mistakeId);
        return ResourceUtils.delete(request, response, command, mistakeAppService::deleteMistake);
    }
}
